/*----------------------------------------------------------------------------
 * Copyright (c) <2013-2015>, <Huawei Technologies Co., Ltd>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *---------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 *---------------------------------------------------------------------------*/

#include "stdio.h"
#include "stdlib.h"
#include "asm/atomic.h"
#include "hisoc/usb.h"
#include "asm/delay.h"
#include "asm/io.h"

extern VOID LOS_Udelay(UINT32 usecs);
extern VOID LOS_Mdelay(UINT32 msecs);
extern VOID platform_hiusb_start_hcd(VOID);
extern VOID platform_hiusb_stop_hcd(VOID);

static atomic_t dev_open_cnt = 0;
static int otg_usbhost_stat = 0;
static int otg_usbdev_stat = 0;

void hiusb_start_hcd(void)
{
    unsigned long flags;

    flags = LOS_IntLock();
    if (atomic_inc_return(&dev_open_cnt) == 1) {
        /* enable phy ref clk to enable phy */
        platform_hiusb_start_hcd();
    }
    LOS_IntRestore(flags);
}

void hiusb_stop_hcd(void)
{
    unsigned long flags;

    flags = LOS_IntLock();
    if (atomic_dec_return(&dev_open_cnt) == 0) {

        /* Disable EHCI clock.
        If the HS PHY is unused disable it too. */
        platform_hiusb_stop_hcd();
    }
    LOS_IntRestore(flags);
}

void hiusb_reset_hcd(void)
{
}

void hiusb_host2device(void)
{
    unsigned int reg;
    unsigned int reg1;

    reg = readl(PERI_USB);/*lint !e160*/
    reg &= ~(USB2_PHY_DPPULL_DOWN);
    reg |= DWC_OTG_EN;     /*lint !e648*/
    writel(reg, PERI_USB);
    reg1 = readl(IO_ADDRESS(0x10080804));/*lint !e160*/
    reg1 &= ~(0x1 << 1);
    writel(reg1, IO_ADDRESS(0x10080804));
}

void hiusb_device2host(void)
{
    unsigned int reg;
    unsigned int reg1;

    reg = readl(PERI_USB);/*lint !e160*/
    reg1 = readl(IO_ADDRESS(0x10080804));/*lint !e160*/
    reg1 |= (0x1 << 1);
    writel(reg1, IO_ADDRESS(0x10080804));
    reg |= USB2_PHY_DPPULL_DOWN;
    reg &= ~DWC_OTG_EN;    /*lint !e648*/
    writel(reg, PERI_USB);
}

void usb_otg_run(void)
{
    unsigned int reg;
    reg = readl(PERI_USB);/*lint !e160*/

    /* device -->host */
    if ((reg & DWC_OTG_EN) == DWC_OTG_EN) {    /*lint !e648*/
        if (otg_usbdev_stat == 1) {
            return;
        }
        hiusb_device2host();

    } else { /* host -->device */
        if (otg_usbhost_stat == 1) {
            return;
        }
        hiusb_host2device();
    }
}

int hiusb_is_device_mode(void)
{
    return ((otg_usbhost_stat == 0)&&(otg_usbdev_stat == 1));
}

void usb_otg_sw_set_host_state(void)
{
    otg_usbhost_stat = 1;
}

void usb_otg_sw_clear_host_state(void)
{
    otg_usbhost_stat = 0;
}

void usb_otg_sw_set_device_state(void)
{
    otg_usbdev_stat = 1;
}

void usb_otg_sw_clear_device_state(void)
{
    otg_usbdev_stat = 0;
}
